<template>
  <div class="add-wrap">
    <el-form
      :inline="true"
      :model="ruleform"
      ref="from"
      :rules="rules"
      class="demo-form-inline ip-form"
    >
      <!-- <el-form-item label="开发者ID" prop="dev_id">
        <el-select v-model="ruleform.dev_id" placeholder="请选择">
          <el-option v-for="item in dev_id_arr" :key="item" :label="item" :value="item" />
        </el-select>
      </el-form-item>-->
      <el-form-item label="开发者ID" prop="dev_id">
        <el-input v-model="ruleform.dev_id" style="width:330px" message disabled></el-input>
      </el-form-item>

      <el-form-item label="应用名称 " prop="app_name">
        <el-input
          v-model="ruleform.app_name"
          style="width:330px"
          message
          placeholder="包含英文开头、数字、中划线、下划线"
        ></el-input>
      </el-form-item>
      <br />
      <!-- <el-form-item label="选择分组" prop="selOrganization">
        <el-checkbox-group v-model="ruleform.selOrganization">
          <el-checkbox
            :label="item.org_id"
            :value="item.org_id"
            :key="item.org_id"
            v-for="item in organizationLabel"
          >{{ item.gfather_name+" - "+item.father_name+" - "+item.org_name }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <br /> -->
      <el-form-item label="选择业务范围--设备" prop="businessRange">
        <el-radio-group v-model="ruleform.businessRange">
          <el-radio label="-1">全部禁止</el-radio>
          <el-radio label="0">全部允许</el-radio>
          <el-radio label="1">指定范围</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item class="btn-wrap">
        <el-button type="primary" plain @click="onSubmit('from')">保存</el-button>
        <el-button type="warning" plain @click="onReset('from')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getData, postData } from "@/request/http";
import { resetForm } from "@/utils/Tool";
export default {
  name: "",
  components: {},
  props: {},
  data() {
    return {
      dev_id_arr: [], //开发者id
      organizationLabel: [], //添加中，组织机构的label
      ruleform: {
        dev_id: this.$route.query.dev_id,
        app_name: "",
        //selOrganization: [],
        businessRange: "-1"
      },
      rules: {
        dev_id: [{ required: false, message: "", trigger: "blur" }],
        app_name: [
          {
            required: true,
            // pattern: /^[a-zA-Z]+$/,
            message: "应用名称只能为英文开头、数字、中划线、下划线",
            trigger: "blur"
          }
        ],
        // selOrganization: [
        //   {
        //     type: "array",
        //     required: false,
        //     message: "请至少选择一个组织机构",
        //     trigger: "change"
        //   }
        // ],
        businessRange: [
          { required: true, message: "请选择业务范围", trigger: "blur" }
        ]
      }
    };
  },
  computed: {},
  created() {},
  mounted() {
    //this.get_dev_id();
    this.getOrgLabel();
  },
  methods: {
    //获取管理员的组织机构的label
    getOrgLabel() {
      postData("/adminweb/org/list", {})
        .then(res => {
          console.log(res);
          if (res.code == 0) {
            let orgs = res.datas.orgs;
            this.organizationLabel = orgs;
          }else if (res.code == -56055) {
            return;
          }  else {
            this.$message.error(res.code + "，" + res.msg);
          }
        })
        .catch(res => {
          this.$message.error(res.code + "，" + res.msg);
        });
    },
    //添加 提交
    onSubmit(formName) {
      var that = this;
      // var org_sub = that.ruleform.selOrganization.map(item => Number(item));
      // console.log(org_sub);
      that.$refs[formName].validate(valid => {
        if (valid) {
          postData("/adminweb/dev/app/register", {
            dev_id: that.$route.query.dev_id,
            app_name: that.ruleform.app_name,
            bizfors: [
              {
                biz_type: 0,
                biz_for: that.ruleform.businessRange
              }
            ]
          })
            .then(res => {
              if (res.code == 0) {
                that.$message.success("注册成功！");
                //刷新table
                that.$store.commit(
                  "setSubmitData",
                  Math.floor(Math.random() * 10000000)
                );
                let datas = resetForm(that.ruleform, this.$refs[formName]);
                that.ruleform = datas;
              } else {
                this.$message.error(res.code + "，" + res.msg);
              }
            })
            .catch(res => {
             this.$message.error(res.code + "，" + res.msg);
            });
        } else {
          that.$message.error("注册失败");
          return false;
        }
      });
    },
    //重置
    onReset(val) {
      this.$refs[val].resetFields();
    }
  },
  watch: {}
};
</script>
<style scoped>
.add-wrap {
  padding: 15px;
}

.add-wrap >>> .btn-wrap {
  margin-left: 40px;
}
</style>
